﻿//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_Logger.h".
//-------------------------------------------------------------------

#ifndef _UXS_LOGGER_CLASS_H
#define _UXS_LOGGER_CLASS_H

#pragma once

#include "ILogger.h"
//#include "../../filesystem/ux_filesystem.h"

namespace UX_STL
{
namespace UXLogger
{

	//
	// Logger
	//

	class UX_EXPORT CLogger : public ILogger
	{
	private:
		typedef array< ILogOutput *, TFastCopyStrategy< ILogOutput *> >	logoutput_array_t;

		logoutput_array_t					_aOutput;
		CallbackProc_t						_sOnFatalError;
		e_log_msg::type						_eWarningLevel,
											_eFlagFilter;
		atomic_int32						_uWarnings,
											_uErrors;

	public:
		CLogger();
		~CLogger();

		bool Create(e_log_output::bits eBits, const char *pLogFileName = null);
		void SetCallback(const CallbackProc_t &sOnFatalError);

		void Attach(ILogOutput *pOutput);
		void Detach(ILogOutput *pOutput);
		void FreeAll();

		void NewLine();
		void Write(const char *pMsg, e_log_msg::type eType);
		void WriteExt(const char *pMsg, e_log_msg::type eType, const char *pSrcFile, uint uLine);
		
		void Write(uni_c_string sMsg, e_log_msg::type eType);
		void WriteExt(uni_c_string sMsg, e_log_msg::type eType, const char *pSrcFile, uint uLine);

		void SetFlagFilter(e_log_msg::type eFlag);
		void SetWarningFilter(uint uLevel);

		e_log_msg::type	GetFlagFilter()		const	{ return _eFlagFilter; }
		e_log_msg::type	GetWarningFilter()	const	{ return _eWarningLevel; }
		uint			GetWarnings()		const	{ return _uWarnings; }
		uint			GetErrors()			const	{ return _uErrors; }
	};
	
		
	
/*
=================================================
	перевести на новую строку
=================================================
*/
	inline void CLogger::NewLine()
	{
		FOR( i, _aOutput ) {
			_aOutput[i]->NewLine();
		}
	}
	
/*
=================================================
	записать в лог
=================================================
*/
	inline void CLogger::Write(const char *pMsg, e_log_msg::type eType)
	{
		WriteExt( pMsg, eType, null, -1 );
	}

/*
=================================================
	установить фильтр для флагов
=================================================
*/
	inline void CLogger::SetFlagFilter(e_log_msg::type eFlag)
	{
		_eFlagFilter = eFlag | e_log_msg::_LWL_MASK;
	}
	
/*
=================================================
	установить пороговый уровень предупреждения
=================================================
*/
	inline void CLogger::SetWarningFilter(uint uLevel)
	{
		_eWarningLevel = (uLevel << e_log_msg::_LWL_OFFSET) & e_log_msg::_LWL_MASK;
	}
	
/*
=================================================
	
=================================================
*/
	inline void CLogger::Write(uni_c_string sMsg, e_log_msg::type eType)
	{
		return Write( sMsg.cstr(), eType );
	}

	inline void CLogger::WriteExt(uni_c_string sMsg, e_log_msg::type eType, const char *pSrcFile, uint uLine)
	{
		return WriteExt( sMsg.cstr(), eType, pSrcFile, uLine );
	}

//-------------------------------------------------------------------

}	// UXLogger
}	// UX_STL

#endif	// _UXS_LOGGER_CLASS_H